package nodes.command;

import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;
import parser.ParserException;
import model.Model;
import nodes.AbstractNode;
import nodes.CloseParensNode;
import nodes.ConstantNode;
import nodes.OpenParensNode;


/**
 * Runs a command(s) if an expression is not 0
 */
public class IfC extends AbstractCommand
{
    public AbstractNode evaluate (List<AbstractNode> myArguments, Model model)
    {
        if (myArguments.size() < Integer.parseInt(resources.getString("if")))
        {
            throw ParserException.BAD_ARGUMENTS;
        }

        AbstractNode expr = myArguments.get(0);

        int exprResult = expr.evaluate().getValue();

        if (exprResult != 0)
        {
            for(int i = 1; i < myArguments.size(); i ++)
            {
                myArguments.get(i).evaluate();
            }
        }

        return new ConstantNode(exprResult);
    }
    
    @Override
    public List<AbstractNode> setUpCommand (Queue<AbstractNode> outputQueue, String name)
    {
        List<AbstractNode> arguments = new ArrayList<AbstractNode>();
        try
        {
            System.out.println(outputQueue + " queue");
            while (!(outputQueue.peek() instanceof OpenParensNode)){
                arguments.add(outputQueue.poll().buildTree(outputQueue));
            }
            outputQueue.poll();
            while (!(outputQueue.peek() instanceof CloseParensNode)){
                arguments.add(outputQueue.poll().buildTree(outputQueue));
            }
            outputQueue.poll();
        }
        catch (NullPointerException e)
        {
            System.out.println(outputQueue + " arguments");
            throw ParserException.BAD_TOKEN;
        }
        
        return arguments;
    }
}
